//
//  LearnMiscCell.m
//  MIDI
//
//  Created by 千行时线 on 15/9/14.
//  Copyright (c) 2015年 QXSX. All rights reserved.
//

#import "LearnMiscCell.h"

#import "SearchMIDISirModel.h"

#import "SearchMIDIModel.h"

#import "UIImageView+WebCache.h"

#import "CommFunc.h"

@interface LearnMiscCell()

@property (weak, nonatomic) IBOutlet UIImageView *head;

@property (weak, nonatomic) IBOutlet UIImageView *head2;

@property (weak, nonatomic) IBOutlet UILabel *titleUILabel;

@property (weak, nonatomic) IBOutlet UILabel *contentUILabel;

@property (assign, nonatomic) int titlefontSize;
@property (assign, nonatomic) int cityfontSize;

@property (assign, nonatomic) int contentfontSize;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconWith;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconHeight;

@end

@implementation LearnMiscCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"LearnMiscCell";
  
    LearnMiscCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:ID owner:nil options:nil]firstObject];
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    return cell;
}

-(void)awakeFromNib
{
    if ([UIScreen mainScreen].applicationFrame.size.width==320)
    {
        if([UIScreen mainScreen].applicationFrame.size.height == 460)
        {
            self.iconWith.constant = 70;
            self.iconHeight.constant = 70;
        }
        else
        {
            self.iconWith.constant = 70;
            self.iconHeight.constant = 70;
        }
    }else if ([UIScreen mainScreen].applicationFrame.size.width==375)
    {
        self.iconWith.constant = 80;
        self.iconHeight.constant = 80;
        
    }else if ([UIScreen mainScreen].applicationFrame.size.width==414)
    {
        self.iconWith.constant = 80;
        self.iconHeight.constant = 80;
    }
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    
    [self addGestureRecognizer:tapGestureRecognizer];
    
    self.titlefontSize = 15;
    self.cityfontSize = 17;
    self.contentfontSize = 10;
    
    if ([[UIScreen mainScreen] bounds].size.width == 320)
    {
        if ([[UIScreen mainScreen] bounds].size.height == 568)
        {
            self.titlefontSize = 15;  //iphone4s、5、 5s
            self.cityfontSize = 17;
            self.contentfontSize = 12;
        }
    }
    else if ([[UIScreen mainScreen] bounds].size.width == 375)
    {
        self.titlefontSize = 15;  //iphone6
        self.cityfontSize = 17;
        self.contentfontSize = 12;
    }
    else if ([[UIScreen mainScreen] bounds].size.width == 414)
    {
        self.titlefontSize = 18;  //iphone6 plus
        self.cityfontSize = 20;
        self.contentfontSize = 12;
    }
    
    [_titleUILabel setFont:[UIFont fontWithName:@"Hiragino Sans GB" size:self.titlefontSize]];
    
    [_contentUILabel setFont:[UIFont fontWithName:@"Hiragino Sans GB" size:self.contentfontSize]];
}

-(void)tap
{
    if (_midiClub) {
        
        if ([self.delegate respondsToSelector:@selector(pushToVC:MidiClub:)]) {
            
            [self.delegate pushToVC:nil MidiClub:_midiClub];
        }
        
    }else{
        
        if ([self.delegate respondsToSelector:@selector(pushToVC:MidiClub:)]) {
            
            [self.delegate pushToVC:_midiSir MidiClub:nil];
        }
    }
}


-(void)setMidiClub:(SearchMIDIModel *)midiClub{

    
    if (midiClub) {
       
        _midiClub = midiClub;
        
        _titleUILabel.text = midiClub.exam_site_name;
        
        if (midiClub.site_icon != nil && ![midiClub.site_icon isEqualToString:@""])
        {
            [self.head sd_setImageWithURL:[NSURL URLWithString:midiClub.site_icon] placeholderImage:[UIImage imageNamed:@"img_learn_misc_cell_samll_loading"]];
        }
        
        _contentUILabel.text = midiClub.address;
    }
}

- (void)setMidiSir:(SearchMIDISirModel *)midiSir{

    if (midiSir) {
        
        _midiSir = midiSir;
        
        NSString *title = [NSString stringWithFormat:@"%@ - %@ - %@", midiSir.real_name, midiSir.major_name, midiSir.name];
        
//        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:title];
//        
//        NSDictionary *attribs = @{NSFontAttributeName: [UIFont fontWithName:@"Hiragino Sans GB" size:self.cityfontSize]};
//        
//        [str addAttributes:attribs range:NSMakeRange(title.length - midiSir.name.length, midiSir.name.length)];
        
//        _titleUILabel.attributedText = str;
        
        _titleUILabel.text = title;
        
        if (midiSir.head_img != nil && ![midiSir.head_img isEqualToString:@""])
        {
            [self.head sd_setImageWithURL:[NSURL URLWithString:midiSir.head_img]];
        }
        
        _contentUILabel.text = midiSir.mr_introduce;
    }
}

-(int)getHeight
{
    int height = [CommFunc sizeWithString:self.contentUILabel.text
                                     font:[UIFont fontWithName:@"Hiragino Sans GB" size:self.contentfontSize]
                                      max:CGSizeMake(self.contentUILabel.frame.size.width, 40)].height;
    
    if (height <= 0)
        return 82;
    else
    {
        height = self.contentUILabel.frame.origin.y + height + 30;
        
        return height;
    }
    
    return 0;
}



@end













